/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class provides the database access. It handles the connection
 * configurations.
 *
 * @author Anton Strack
 *
 * Parts of this code originated from code provided by Mr. Bell's AIT 618 class
 */
public class Datasource {

    private Connection connection = null;
    private static final Logger log = Logger.getLogger(Datasource.class.getName());
    private String url, user, password;
    private String basepath;

    public Datasource(String url, String user, String password, String basepath) {
        this.url = url;
        this.user = user;
        this.password = password;
        this.basepath = basepath;
    }

    //------------------- Uncomment this section to use MySQL as the database. The program functions properly with MySql but the database and data must be manually loaded first.
    /**
     * Get a connection for the MySql database.
     *
     * @return
     */
    public Connection getConnection() {
        if (this.connection == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();

                String connString = this.url + "?user=" + this.user + "&password=" + this.password;
                String openshift_homedir = System.getenv("OPENSHIFT_HOMEDIR");
                if (openshift_homedir == null || openshift_homedir.isEmpty()) {
                    // Local testing
                    connString = "jdbc:mysql://localhost/csdapp_db?user=csdapp_db_user&password=0csdwAppPass0";
                }
                this.connection = DriverManager.getConnection(connString);
            } catch (SQLException ex) {
                log.log(Level.WARNING, "Exceoption caught when trying to connect to database. SQLState:{0} Exception:{1}", new Object[]{ex.getSQLState(), ex.getMessage()});
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                log.log(Level.WARNING, "{0} Failed to load the com.mysql.jdbc.Driver{1}", new Object[]{this.getClass(), ex.getMessage()});
            } catch (InstantiationException ex) {
                log.log(Level.WARNING, "{0} Failed to load the com.mysql.jdbc.Driver{1}", new Object[]{this.getClass(), ex.getMessage()});
            } catch (IllegalAccessException ex) {
                log.log(Level.WARNING, "{0} Failed to load the com.mysql.jdbc.Driver{1}", new Object[]{this.getClass(), ex.getMessage()});
            }
        }
        return this.connection;
    }
    /**
     * Get a connection from the Hsqldb database. This is the default database
     * and it comes pre loaded with the tables, constraints, and data.
     *
     * @return a connection to the database.
     */
//    public Connection getConnection() {
//
//        String url = "jdbc:hsqldb:file:";
//        String driver = "org.hsqldb.jdbcDriver";
//        String uri = url + basepath + "/db/" + "csdapp_db" + ";shutdown=true";
//        log.log(Level.INFO, "Database URI: {0}", uri);
//        try {
//            Class.forName(driver).newInstance();
//            this.connection = DriverManager.getConnection(uri, "sa", "");
//        } catch (Exception e) {
//            log.warning(e.getMessage());
//        }
//        return this.connection;
//    }
}
